package com.bls.dao.impl;

import com.bls.dao.G4Dao;
import com.bls.entity.Draft;
import com.bls.entity.G4;
import com.bls.entity.vo.DraftVo;
import com.fy.toolhelper.db.BaseDaoImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author xiaoliang
 * @date 2022/01/10 20:12
 **/
public class G4DaoImpl extends BaseDaoImpl<G4> implements G4Dao {
    public G4DaoImpl() throws Exception {
    }

    @Override
    public List<G4> pageConditionG4List(Connection connection, G4 g4, Integer pageSize, Integer currentPage) throws SQLException {
        int start = pageSize * (currentPage - 1);
        StringBuilder sql = new StringBuilder("SELECT * FROM bls_g4 WHERE 1 = 1 ");

        if (!StringUtils.isEmpty(g4.getName())) {
            sql.append(" and name like '%").append(g4.getName()).append("%'");
        }
        if (!StringUtils.isEmpty(g4.getContent())) {
            sql.append(" and content like '%").append(g4.getContent()).append("%'");
        }
        if (!StringUtils.isEmpty(g4.getExample())) {
            sql.append(" and example like '%").append(g4.getExample()).append("%'");
        }
        sql.append(" limit ? , ?");

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        pstm.setInt(1, start);
        pstm.setInt(2, pageSize);
        ResultSet rs = pstm.executeQuery();
        List<G4> g4List = new ArrayList<>();
        while (rs.next()) {
            G4 g4Entity = getG4Entity(rs);
            g4List.add(g4Entity);
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return g4List;
    }

    @Override
    public int pageConditionG4Total(Connection connection, G4 g4Query) throws SQLException {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) as count FROM bls_g4 WHERE 1 = 1 ");

        if (!StringUtils.isEmpty(g4Query.getName())) {
            sql.append(" and name like '%").append(g4Query.getName()).append("%'");
        }
        if (!StringUtils.isEmpty(g4Query.getContent())) {
            sql.append(" and content like '%").append(g4Query.getContent()).append("%'");
        }
        if (!StringUtils.isEmpty(g4Query.getExample())) {
            sql.append(" and example like '%").append(g4Query.getExample()).append("%'");
        }

        PreparedStatement pstm = connection.prepareStatement(sql.toString());
        ResultSet rs = pstm.executeQuery();
        int count = -1;
        while (rs.next()) {
            count = rs.getInt("count");
        }
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        return count;
    }

    private G4 getG4Entity(ResultSet rs) throws SQLException {
        G4 g4 = new G4();
        g4.setId(rs.getLong("id"));
        g4.setName(rs.getString("name"));
        g4.setContent(rs.getString("content"));
        g4.setExample(rs.getString("example"));
        return g4;
    }
}
